<?

class sign_in_action extends action_controller
{
	public function execute()
	{
		helper_html::$meta['title'] = 'Signin';
		if ( session::user_id() ) response::redirect ('/');
	}

	public function execute_post()
	{
		$this->json = array();

		if ( $_POST['email'] && $_POST['password'] )
		{
			if ( $user = user_peer::get_by('email', $_POST['email']) )
			{
				if ( $user['password'] == md5($_POST['password']) )
				{
					return session::authorize($user['id']);
				}
			}
		}
		
		$this->json = array('error' => 'Wrong email or password');
	}
}